package com.rose.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.rose.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117606100";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC/Op/nJY1E8ELHmx24P7CGgOqD8GAi2KGPIbDZ7aHwvTfjf2ufYzON7N4LixbJbzO+mvD7CHBJKIHgtKqcboWSwEDpbTkGhf4a7EVwDfgK8WOLI1XOs9TvCLoTtceaKuw1n9frLuwLzNi3H8Rz3MzDGuP86F+SN+A38lSlzysDbZuS/lFraFfPgo0PCp4sO5zvC7tka5LikNY2g2DkUTdbZ0ruZEGNk1AFn0PLJk4LEQJPVzdttigMM6HtPvczzQV+4h9Vm4qeKPCsDTzeHjX0aLmpDTJP+FbT8GYTqqrk+IL9aBaz0L7FO7bORTP+gSHhYaglgSV8rVqSODf+y/mxAgMBAAECggEADyZN1Loc3wFJyRZ43EfvbDh6W8mHVDtlUTmSETHKSpAzBUrq882gH/ZMAQ4b2Al/R/JudPTOMH2NDIhDm/EFlAK3IccNZX7oCqn9NktlAzjscfFystV8YROH6lfUYFpWLEllibRF7wljgd56adNsRWiz9y1RpPvYP5ZrLlejSLoa/yLdperVCSO9Qcsef+QioYvCeSOR83r2KDXfIxigwwS3ZHX/729p0R99B1TKUHgqoox/vb0pfavucbF2p6wh2PIoqasSErVZidEGNCg9Rk+bOJyJjrHtjLgFySKrYBrxZM3AMO5H2CwDMAe8txfcm07IIZ2D2MpTbzMoIm/ooQKBgQD2M/6ZEHztOJXSR0U0NoYiiDmrn+TIUQFC0hvp56ZkYxPEG4zjneE4bZg5BreCJXgqu2EpU1kTJ/JKddiBW2ZRZ2jkvlRj1m9asufFAwzo8QpPK3uzBrZW0UZuMr/qIen8U/MRHgg+NGqyQ1GDnVBId1kaTKm8NIWbaS0MAQq3IwKBgQDG1p+h1S0k+FD/7miz9/IZewRaeONV23nM03D2vfX5clYrQKC3NYvEu3bDbfU5S6NW2jdr31ks0EPmYCHO74lXJRT1X3nZ+KpjIUEUMraBjADa5hw829KOFIi18qUbWW0e7uxLd+FQhMrvb/wiCi97xcYdzYSzhcevs5DsmlnDGwKBgQDeCjxmDPGt+6da8n3ePgIw6IyiVNy/MBckDVuxUwLEbHpqFbK1oTB6dJShcYZUsLg0SbOx0oJ1/UPNlYSB/U1Ewy9dhK2VMNJSiSDvk/Wzq5dxmTTBW59covaS8eAVJyrgL1H6sQwcFafYR1Afk+PQxxor1tIJc5C/fVZsChCA4wKBgFTUjBZHFJOJPUVzOUhOqAMm8JyIprSihjxUmQUr1h0uIoUhdW+0cITmVaza5JwlIPKU9sAW8bykvdMzgDkBCN8c+uwUZ7AO3uKb9V/gouxyBotdUP7F6faj3A+EDdy1MlDYLgekhYQMo2ezpyX+onNz1Sox3EOa966GNXY8A7+xAoGBAJTBp24O3WBceWy4uD7NIqrVZosqmrsBMXvWJzCAsNDz62HzB+zqVw9laLTbUwXeVVXvHVmYzrE2TiDeQaPqCSAliJgeg0Fg/6gryO3lkeB4CeVwGHefzF32471ctc0x2XKLl+XaiyYxpHWCGgWmmTmq5HzjDYdLGVxt9ZyhyNM1";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvFNnc9cYFdBKR+CV6PjttB7kPPmY0hFRHFpPyOsCfYRfqA89MsuqiYrt4ywKilceFJAUk1yyl7MUbZ0zOVMhPGkVfPTW4Hi+puEvrIzkStLxvSmqpwI9mn4E3swMCIAy6j3yIyKkEJJU66UtcH/Jmhfxyq6DATAWU4Rq75nvVtPQV+hJSen49WQMUNPBBqFdduRQCBR6VXyOf119uFBIP9Lb3/GP/I6o/H7jaWK+AWPLsTMlnnF1WtGY8x4om9PIhwOELovJK8rUOxsecNnIPojy0Il5ufPpZdWyvkULdls98uqBo0CvTX+HDgLvK0+Diho+kbIj6trBJ9Z9MH0FdQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://order.mall.com/payed/notify";
//    private  String notify_url = "http://应该为内网穿透的地址 order.mall.com:80穿透的  /payed/notify";


    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.mall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 自动关单时间
    private String timeout = "15m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        // 30分钟内不付款就会自动关单
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        return result;
    }
}
